fix: evaluate window aggregate arguments after FILTER - #24508
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #24508 +/- ##
==========================================
- Coverage 81.44% 81.44% -0.01%
==========================================
Files 1118 1118
Lines 399560 399634 +74
Branches 399560 399634 +74
==========================================
+ Hits 325424 325483 +59
- Misses 55137 55143 +6
- Partials 18999 19008 +9 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
neilconway
left a comment
There was a problem hiding this comment.
This approach makes sense and is simple and clean, but I'd be curious to understand how it influences performance, especially for simple and infallible expressions (e.g., bare column references).
|
Thanks for the review! I think this could cause a performance regression when the window argument is a direct column or another cheap expression. In these cases, the cost of filtering the entire batch and scattering the result back to the original row positions may exceed the expression evaluation cost that we skip. As a low-risk mitigation, we could bypass This could address the most obvious regressions for direct columns and literals. However, it would only be a temporary mitigation and would not cover other cheap expressions such as One possible long-term direction might be to keep the selected argument values in compact form instead of scattering them back. We could maintain a prefix-count array that maps each boundary in the original row space to the corresponding boundary in the compact argument arrays: Window frame calculation and output rows could remain in the original row space, while only the ranges passed to the accumulator would be mapped into the compact arrays. This could allow us to skip evaluating expressions for excluded rows while still producing the correct window results. It could also avoid per-argument scatter and the additional filtering before I'm not sure whether this approach would improve performance in practice. does this direction make sense to you? |
|
@lyne7-sc Yep, that all makes sense -- both the short-term mitigation and the longer-term optimization possibility. Should we start by adding a micro-benchmark to cover the window agg + FILTER case and then proceed from there? |
|
@neilconway yes, that makes sense. I've added the microbenchmark separately in #24589. Here are the results from my local: |
Which issue does this PR close?
Rationale for this change
Window aggregates evaluate their arguments before applying
FILTER, which can cause errors on rows that should not participate in the aggregate.This is analogous to #24444 for grouped aggregates, window aggregates use a separate evaluation path.
What changes are included in this PR?
Evaluate the window aggregate filter before evaluating its arguments.
Use the resulting filter mask as a selection when evaluating argument expressions. Selected results are scattered back to the original batch length so that window frame row indices remain aligned.
This applies to both ever-expanding and sliding aggregate window frames.
Are these changes tested?
Yes.
Are there any user-facing changes?
Window aggregate arguments are no longer evaluated for rows rejected by
FILTER, no public API changes.